from django.shortcuts import render,redirect
#导包
from django.http import HttpResponse,HttpResponseRedirect,JsonResponse
#导入类视图
from django.views import View

from myapp.models import User,Goods
import json
from django.core.serializers import serialize
from rest_framework.response import Response
from rest_framework.views import APIView
#导入加密库
import hashlib
#导入图片库
#绘画库
from PIL import ImageDraw
#字体库
from PIL import ImageFont
#图片库
from PIL import Image
#随机库
import random
#文件流
import io

import requests

#导入上传文件夹配置
from mydjango.settings import UPLOAD_ROOT
import os

#导入原生sql模块
from django.db import connection

import jwt

#导入redis数据库
import redis

#导入时间模块
import time

#导入公共目录变量
from mydjango.settings import BASE_DIR

#导包
from django.db.models import Q,F

#导入dwebsocket的库
# from dwebsocket.decorators import accept_websocket

#定义地址和端口
host = '127.0.0.1'
port = 6379

#建立redis连接
r = redis.Redis(host=host,port=port)

from myapp.models import *

#商品入库接口
class AddGoods(APIView):
    def get(self,request):
        name = request.GET.get('name')
        desc = request.GET.get('desc')
        img = request.GET.get('img')
        video = request.GET.get('video')
        param = request.GET.get('param')
        price = request.GET.get('price')
        cid = request.GET.get('cid')

        #排重
        goods = Goods.objects.filter(name=name).first()

        if goods:
            res = {}
            res['code'] = 405
            res['message'] = '该商品已经存在'
            return Response(res)

        #进行入库
        goods = Goods(name=name,desc=desc,img=img,video=video,param=param,price=price,cid=cid)
        goods.save()
        
        res = {}
        res['code'] = 200
        res['message'] = '商品添加成功'

        return Response(res)






class CategoryList(APIView):
    def post(self, request):
        resp = {}
        # 获取数据
        cname = request.data.get('cname')

        # 排重
        goods =Category.objects.filter(cname=cname).first()
        if goods:
            return Response(
                {
                    'code': 405,
                    'message': '商品分类已存在'
                }
            )
        # 进行入库
        cate = Category(cname=cname)
        cate.save()
        return Response(
            {
                'code': 200,
                'message': '分类添加成功'
            }
        )

    # 修改商品分类
    def put(self, request):
        resp = {}
        # 获取修改的商品分类id
        id = request.query_params.get('id')
        # 获取数据
        cname = request.data.get('cname')

        # 排重
        cates = Category.objects.filter(cname=cname).first()
        if cates:
            return Response(
                {
                    'code': 405,
                    'message': '分类已存在'
                }
            )
        # 进行修改入库
        cate = Category.objects.get(id=id)
        cate.cname = cname
        cate.save()
        return Response(
            {
                'code': 200,
                'message': '分类修改成功'
            }
        )

    # 删除商品分类
    def delete(self, request):
        # 获取删除的商品分类id
        id = request.query_params.get('id')
        # 进行查询并删除
        Category.objects.get(id=id).delete()
        return Response({'message': '删除成功'})

# 商品列表接口
class GoodsInfo(APIView):
    def get(self, request):
        goods = models.Goods.objects.all()
        # 序列化
        goods_ser = GoodsSer(goods, many=True)
        # 返回数据
        return Response(
            goods_ser.data
        )


# 分类列表接口
class CateInfo(APIView):
    def get(self, request):
        cate = models.Category.objects.all()
        # 序列化
        cate_ser = CategorySer(cate, many=True)
        # 返回数据
        return Response(
            {
                'code': 200,
                'data': cate_ser.data
            }
        )
        
# 添加商品分类
class AddCate(APIView):
    def get(self, request):
        cname = request.GET.get('cname')

        cates = Category.objects.filter(cname=cname)
        if cates:
            return Response({'code': 301, 'message': '该分类已存在'})
        cate = Category(cname=cname)
        cate.save()
        return Response({'code': 200, 'message': '创建成功'})